﻿using ConfExpert.Datasets;
using ConfExpert.Forms.Base;
using ConfExpert.Utility;

using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;


using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace ConfExpert.Forms.Planlama
{
    public partial class KesilenGomlekDepo : XForm
    {
        public KesilenGomlekDepo()
        {

            InitializeComponent();
        }

        private void KesilenGomlekDepo_Load(object sender, EventArgs e)
        {
            btnTransfer.Enabled = false;

            gleAtolye.Edit.Assign(RI(Rp.Atelye));
            DataTable dtDepo = Session.DataManager.ExecuteDataTableByQuery("select DepoId,Name from Depo where DepoId not in (4,5,8)");
            RepositoryItemGridLookUpEdit riDepo = GetCustomGLE(dtDepo, "Name", "DepoId");
            gleDepo.Edit.Assign(riDepo);
            deYukleme.EditValue = DateTime.Now;
            gridView1.RowStyle += gridView1_RowStyle;
            gleDepo.EditValueChanged += gleDepo_EditValueChanged;
            gridView1.FocusedRowChanged += gridView1_FocusedRowChanged;
            gridView1.RowCellStyle += gridView1_RowCellStyle;
            gleAtolye.EditValueChanged += gleAtolye_EditValueChanged;
            cmbTransfer.EditValueChanged += cmbTransfer_EditValueChanged;
            gleAktarılacakDepo.EditValueChanged += gleAktarılacakDepo_EditValueChanged;
            btnTransfer.Click += btnTransfer_Click;

            cmbTransfer.Text = "Depo";
            this.Shown += KesilenGomlekDepo_Shown;
        }

        void KesilenGomlekDepo_Shown(object sender, EventArgs e)
        {
            gleAktarılacakDepo.Focus();
        }

        void btnTransfer_Click(object sender, EventArgs e)
        {
            gridView1.EndEditing();
            if (gleAktarılacakDepo.EditValue == null)
            {
                XtraMessageBox.Show("Lütfen transfer edilecek depoyu veya nakliyeciyi seçin.");
                return;
            }

            else if (gridView1.DataTable().Select("Seç=1").Count() == 0)
            {
                XtraMessageBox.Show("Lütfen transfer yapacağınız modelleri seçin!");
                return;
            }
            else
            {
                DataTable dt = gridView1.DataTable().Select("Seç=1").CopyToDataTable();
                dt.TableName = "GomlekStok";
                string xml = dt.Serialize();
                if (cmbTransfer.Text == "Depo")
                    ExecuteNonQuery("InsGomlekStok", dt.Serialize(), gleAktarılacakDepo.EditValue.ToInt(), gleDepo.EditValue.ToInt(), cmbTransfer.EditValue.ToString(), WhoAmI);
                else
                {
                    if (deYukleme.EditValue == null || deInvoice.EditValue == null || txtInvoice.EditValue == null)
                    {
                        XtraMessageBox.Show("Gömlekler nakliyeciye transfer edilecekse InvoiceNo,Invoice Tarihi ve Yükleme Tarihi alanları boş bırakılamaz", "DİKKAT");
                        return;
                    }
                    else
                        ExecuteNonQuery("UpdGomlekStok", dt.Serialize(), gleAktarılacakDepo.EditValue.ToInt(), gleDepo.EditValue.ToInt(), cmbTransfer.EditValue.ToString(), WhoAmI, txtInvoice.Text, deInvoice.EditValue, deYukleme.EditValue);
                }
                if (gleAtolye.EditValue != null)
                {
                    gridView1.RefreshDataSource("GetModel", gleAtolye.EditValue);
                    gridView1.BestFitColumns();
                    gridView1.MakeReadOnly();
                }
                else
                {
                    gridView1.RefreshDataSource("GetGomlekStok", gleDepo.EditValue);
                    gridView1.BestFitColumns();
                    gridView1.MakeReadOnly();
                }
            }
        }

        void gleAktarılacakDepo_EditValueChanged(object sender, EventArgs e)
        {
            btnTransfer.Enabled = true;
            if (gleDepo.EditValue.ToInt() == 6 && gleAktarılacakDepo.EditValue.ToInt() != 8)
            {
                if (XtraMessageBox.Show("Karbel Depo'daki gömlekleri Karbel mağaza dışında bir depoya atmak istediğinize emin misiniz?", "DİKKAT", MessageBoxButtons.YesNo) == DialogResult.No)
                    gleAktarılacakDepo.EditValue = 8;
            }
        }

        void cmbTransfer_EditValueChanged(object sender, EventArgs e)
        {
            if (cmbTransfer.Text == "Nakliyeci")
            {
                pnl.Visible = true;
            }
            else
            {
                pnl.Visible = false;
                deYukleme.EditValue = null;
                deInvoice.EditValue = null;
                txtInvoice.EditValue = null;
            }

            DataTable dtDepo = Session.DataManager.ExecuteDataTableByQuery("select DepoId,Name from Depo where DepoId not in (4,5,8)");
            RepositoryItemGridLookUpEdit riDepo = GetCustomGLE(dtDepo, "Name", "DepoId");
            if (cmbTransfer.Text == "Depo")
            {
                if (gleDepo.EditValue.ToInt() == 6)
                {
                    DataTable dtDepoKarbel = Session.DataManager.ExecuteDataTableByQuery("select DepoId,Name from Depo where DepoId not in (4,5)");
                    RepositoryItemGridLookUpEdit riDepoKarbel = GetCustomGLE(dtDepoKarbel, "Name", "DepoId");
                    gleAktarılacakDepo.Properties.Assign(riDepoKarbel);
                }
                else
                    gleAktarılacakDepo.Properties.Assign(riDepo);
            }
            else if (cmbTransfer.Text == "Nakliyeci")
                gleAktarılacakDepo.Properties.Assign(RI(Rp.Nakliyeci));
        }

        bool inProgress = false;
        void gleAtolye_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (!inProgress)
                {
                    inProgress = true;
                    if (gleAtolye.EditValue != null)
                        gleDepo.EditValue = null;
                    gridView1.Init("GetModel", gleAtolye.EditValue);
                    ShowOnlyEmptyColumns();



                    DagilimChanged();
                    GridManager gm = GridManager.GetManager(gridView1);
                    gm.GridMenu.RemoveMenu("Seç");
                    gm.GridMenu.RemoveMenu("Bırak");
                    gridView1.MakeColumnInvisible("GirilebilirAdet");
                    gridView1.FormatAsNumber(0, "A01", "A02", "A03", "A04", "A05", "A06", "A07", "A08", "A09", "A10", "A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20", "A21", "A22", "A23", "A24", "A25", "A26", "A27", "A28", "Numune", "Iptal", "Kalite2", "Kalite3", "Kalan1Kalite", "EksikAdet");
                    gridView1.EndEditing();
                    gridView1.AddColumn("Seç", typeof(bool));
                    DataTable dtDepo = Session.DataManager.ExecuteDataTableByQuery("select DepoId,Name from Depo where DepoId not in (4,5,8)");
                    RepositoryItemGridLookUpEdit riDepo = GetCustomGLE(dtDepo, "Name", "DepoId");
                    gleDepo.Edit.Assign(riDepo);
                    gm.GridMenu.AddMenu("Seç", Sec);
                    gm.GridMenu.AddMenu("Bırak", Birak);
                    gridView1.MakeReadOnly();
                    gridView1.AddSummary("KesilenAdet", "Yuklenen", "EksikAdet", "Numune", "Iptal", "Kalite2", "Kalite3", "Fark");
                    gridView1.OptionsView.EnableAppearanceOddRow = gridView1.OptionsView.EnableAppearanceEvenRow = false;
                    gridView1.BestFitColumns();
                    //if (gleAktarılacakDepo.EditValue.ToInt() == 6)
                    //{
                    //    gridView1.MakeColumnInvisible("A01", "A02", "A03", "A04", "A05", "A06", "A07", "A08", "A09", "A10", "A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20", "A21", "A22", "A23", "A24", "A25", "A26", "A27", "A28");
                    //    cmbTransfer.SelectedIndex = 0;
                    //    gleAktarılacakDepo.EditValue = 8;
                    //}
                    //else
                    //{
                    //    gridView1.MakeColumnInvisible("Kalan1Kalite", "Numune", "EksikAdet", "Kalite2", "Kalite3", "Iptal");
                    //    cmbTransfer.SelectedIndex = 1;
                    //}
                }
            }
            finally
            {
                inProgress = false;
            }
        }

        void gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                if (e.Column.FieldName.Length == 3 &&
                    e.Column.FieldName.StartsWith("A"))
                {
                    DataRow dr = gridView1.GetDataRow(e.RowHandle);
                    int D = intParse(dr["DagilimId"]);

                    e.Appearance.ForeColor = (DagilimId != D ? Color.LightSlateGray : Color.Black);
                }
            }
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            DagilimChanged();
        }

        void gleDepo_EditValueChanged(object sender, EventArgs e)
        {

            try
            {
                if (!inProgress)
                {
                    inProgress = true;
                    if (gleDepo.EditValue != null)
                        gleAtolye.EditValue = null;

                    if (gleDepo.EditValue.ToInt() != 6)
                    {
                        gleAktarılacakDepo.EditValue = null;
                    }
                    gridView1.Init("GetGomlekStok", gleDepo.EditValue);
                    gridView1.OptionsView.EnableAppearanceOddRow = gridView1.OptionsView.EnableAppearanceEvenRow = false;
                    GridManager gm = GridManager.GetManager(gridView1);
                    DagilimChanged();
                    gm.GridMenu.RemoveMenu("Seç");
                    gm.GridMenu.RemoveMenu("Bırak");
                    gridView1.EndEditing();
                    gridView1.AddColumn("Seç", typeof(bool));
                    gm.GridMenu.AddMenu("Seç", Sec);
                    gm.GridMenu.AddMenu("Bırak", Birak);
                    gridView1.BestFitColumns();
                    gridView1.MakeReadOnly();
                    gridView1.FormatAsNumber(0, "A01", "A02", "A03", "A04", "A05", "A06", "A07", "A08", "A09", "A10", "A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20", "A21", "A22", "A23", "A24", "A25", "A26", "A27", "A28", "Numune", "Iptal", "Kalite2", "Kalite3", "Kalan1Kalite", "EksikAdet");
                    DataTable dtDepo = Session.DataManager.ExecuteDataTableByQuery("select DepoId,Name from Depo where DepoId not in (4,5,8)");
                    RepositoryItemGridLookUpEdit riDepo = GetCustomGLE(dtDepo, "Name", "DepoId");
                    if (gleDepo.EditValue.ToInt() == 6)
                    {
                        DataTable dtDepoKarbel = Session.DataManager.ExecuteDataTableByQuery("select DepoId,Name from Depo where DepoId not in (4,5)");
                        RepositoryItemGridLookUpEdit riDepoKarbel = GetCustomGLE(dtDepoKarbel, "Name", "DepoId");
                        gleAktarılacakDepo.Properties.Assign(riDepoKarbel);
                        gridView1.MakeColumnInvisible("A01", "A02", "A03", "A04", "A05", "A06", "A07", "A08", "A09", "A10", "A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20", "A21", "A22", "A23", "A24", "A25", "A26", "A27", "A28");
                        cmbTransfer.SelectedIndex = 0;
                        gleAktarılacakDepo.EditValue = 8;
                    }
                    else
                    {
                        gleAktarılacakDepo.Properties.Assign(riDepo);
                        gridView1.MakeColumnInvisible("Kalan1Kalite", "Numune", "EksikAdet", "Kalite2", "Kalite3", "Iptal");
                        cmbTransfer.SelectedIndex = 1;
                    }
                }
            }
            finally
            {
                inProgress = false;
            }
        }
        /*
        void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEdit();
            if (gleAktarılacakDepo.EditValue == null)
            {
                XtraMessageBox.Show("Lütfen transfer edilecek depoyu veya nakliyeciyi seçin.");
                return;
            }

            else if (gridView1.DataTable().Select("Seç=1").Count() == 0)
            {
                XtraMessageBox.Show("Lütfen transfer yapacağınız modelleri seçin!");
                return;
            }
            else
            {
                DataTable dt = gridView1.DataTable().Select("Seç=1").CopyToDataTable();
                dt.TableName = "GomlekStok";
                string xml = dt.Serialize();
                ExecuteNonQuery("InsGomlekStok", dt.Serialize(), gleDepo.EditValue.ToInt(), WhoAmI);

                gridView1.VeriyiGuncelle("GetModel");
                gridView1.BestFitColumns();
                //gridView1.SutunEkle("Seç", typeof(bool));
                //gridView1.OptionsView.EnableAppearanceOddRow = gridView1.OptionsView.EnableAppearanceEvenRow = false;
                //gridView1.MakeReadOnly();
                //gridView1.FormatAsNumber(0, "A01", "A02", "A03", "A04", "A05", "A06", "A07", "A08", "A09", "A10", "A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20", "A21", "A22", "A23", "A24", "A25", "A26", "A27", "A28", "Numune", "Iptal", "Kalite2", "Kalite3", "Kalan1Kalite", "EksikAdet");
                //gridView1.AddSummary();
                //gridView1.AddSummary("KesilenAdet", "Yuklenen", "KalanKalite1", "EksikAdet", "Numune", "Iptal", "Kalite2", "Kalite3", "Fark");
            }

        }
        */

        void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            if (e.RowHandle > -1)
                if ((sender as DevExpress.XtraGrid.Views.Grid.GridView).GetRowCellValue(e.RowHandle, "Seç") != null)
                    if ((sender as DevExpress.XtraGrid.Views.Grid.GridView).GetRowCellValue(e.RowHandle, "Seç") != DBNull.Value)
                        if ((bool)(sender as DevExpress.XtraGrid.Views.Grid.GridView).GetRowCellValue(e.RowHandle, "Seç") == true)
                            e.Appearance.BackColor = Color.SkyBlue;
                        else
                            e.Appearance.Combine((e.RowHandle & 1) != 0 ? view.Appearance.EvenRow : view.Appearance.OddRow);


        }

        void Sec(object sender, EventArgs e)
        {
            gridView1.EndEditing();
            if (gleAktarılacakDepo.EditValue.ToInt() == 0)
            {
                XtraMessageBox.Show("Lütfen transfer edilecek depoyu seçin.");
                return;
            }

            foreach (int handle in gridView1.GetSelectedRows())
                gridView1.SetRowCellValue(handle, "Seç", true);

            if (gleAktarılacakDepo.EditValue.ToInt() != 6)
                gridView1.MakeReadOnly("A01", "A02", "A03", "A04", "A05", "A06", "A07", "A08", "A09", "A10", "A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20", "A21", "A22", "A23", "A24", "A25", "A26", "A27", "A28");

            if (gleAktarılacakDepo.EditValue.ToInt() == 5 || gleAktarılacakDepo.EditValue.ToInt() == 8)
                gridView1.MakeReadOnly("Kalan1Kalite", "EksikAdet", "Numune", "Iptal", "Kalite2", "Kalite3");
        }

        void Birak(object sender, EventArgs e)
        {
            gridView1.EndEditing();
            foreach (int handle in gridView1.GetSelectedRows())
                gridView1.SetRowCellValue(handle, "Seç", false);
        }

        int DagilimId = 0;
        private void DagilimChanged()
        {
            try
            {
                gridView1.CloseEditor();
                gridView1.BeginUpdate();
                if (DagilimId != gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DagilimId").ToInt(-1))
                {
                    DagilimId = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DagilimId").ToInt(-1);

                    MyData.DagilimRow r = AppSession.Lookups.Dagilim.FindByDagilimId(DagilimId);
                    if (r != null)
                        for (int i = 1; i < 29; i++)
                        {
                            string fieldName = String.Format("A{0:00}", i);
                            string cap = r[String.Format("A{0:00}", i)].ToString();
                            gridView1.Columns[fieldName].Caption = cap != string.Empty ? cap : " ";
                        }
                }

            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            finally
            {
                gridView1.EndUpdate();
            }
        }


        Dictionary<int, GridColumn> visibleGridColumns = new Dictionary<int, GridColumn>();
        public void ShowOnlyEmptyColumns(bool state = true)
        {
            if (state)
            {
                visibleGridColumns.Clear();
                foreach (GridColumn gc in gridView1.VisibleColumns)
                    visibleGridColumns.Add(gc.VisibleIndex, gc);

                List<string> columnsToRemove = new List<string>();
                for (int j = 0; j < gridView1.VisibleColumns.Count; j++)
                {
                    string colName = gridView1.VisibleColumns[j].FieldName;
                    columnsToRemove.Add(colName);
                    for (int i = 0; i < gridView1.RowCount; i++)
                    {
                        string val = gridView1.GetRowCellValue(i, colName).ToStr();
                        if (val != "")
                        {
                            columnsToRemove.Remove(colName);
                            break;
                        }
                    }
                }
                foreach (string colName in columnsToRemove)
                    gridView1.Columns[colName].Visible = false;
            }
            else
            {
                foreach (KeyValuePair<int, GridColumn> pairs in visibleGridColumns)
                    pairs.Value.VisibleIndex = pairs.Key;
            }
        }
    }
}
